package api.controller;

import api.model.mltd.MLTDBgInfo;
import api.model.mltd.MLTDCardInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author xiakai
 * @create 2021-04-03 13:39
 */
@RequestMapping("/mltd")
@Controller
public class MLTDController {

    static Random random;

    static ArrayList<MLTDCardInfo> cardInfoList;
    static ArrayList<MLTDCardInfo> bigCardInfoList;
    static ArrayList<MLTDBgInfo> bgInfoList;

    @PostConstruct
    public void init() throws Exception {

        random = new Random();

        cardInfoList = dealCardInfo("mltd-card.txt");
        bigCardInfoList = dealCardInfo("mltd-big-card.txt");
        dealBgInfo();

    }

    @RequestMapping("/card")
    @ResponseBody
    public ModelAndView randomCard(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomCardInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomCardInfo());
            return mav;
        }
        return null;
    }

    @RequestMapping("/big-card")
    @ResponseBody
    public ModelAndView randomBigCard(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomBigCardInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomBigCardInfo());
            return mav;
        }
        return null;
    }

    @RequestMapping("/bg")
    @ResponseBody
    public ModelAndView randomBg(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomBgInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomBgInfo());
            return mav;
        }
        return null;
    }

    public MLTDCardInfo getRandomCardInfo() {
        return cardInfoList.get(random.nextInt(cardInfoList.size()));
    }

    public MLTDCardInfo getRandomBigCardInfo() {
        return bigCardInfoList.get(random.nextInt(bigCardInfoList.size()));
    }

    private MLTDBgInfo getRandomBgInfo() {
        return bgInfoList.get(random.nextInt(bgInfoList.size()));
    }

    private ArrayList<MLTDCardInfo> dealCardInfo(String txtPath) throws IOException {
        // 大图
        ArrayList<MLTDCardInfo> cardInfoList = new ArrayList<>();
        File file = new File(txtPath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String address = br.readLine();
        String line = null;
        while ((line = br.readLine()) != null) {
            MLTDCardInfo cardInfo = new MLTDCardInfo();
            String[] infoGroup = line.split("-");
            try {
                cardInfo.setId(infoGroup[1]);
                cardInfo.setIdolName(infoGroup[2]);
                cardInfo.setCollection(infoGroup[3]);
                cardInfo.setRarity(infoGroup[4]);
                cardInfo.setStatus(infoGroup[5]);
                cardInfo.setSkill(infoGroup[6].substring(0, infoGroup[6].lastIndexOf('.')));
            } catch (IndexOutOfBoundsException e) {
                System.out.println("文件名：" + line + " 有问题（是否没有将'-'替换为'_'）");
                throw e;
            }
            cardInfo.setName(line);
            cardInfo.setUrl(address + URLEncoder.encode(line, "UTF-8").replaceAll("\\+", "%20"));
            cardInfoList.add(cardInfo);
        }
        br.close();
        return cardInfoList;
    }

    private void dealBgInfo() throws IOException {
        // 背景
        bgInfoList = new ArrayList<>();
        File file = new File("mltd-bg.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String address = br.readLine();
        String line = null;
        while ((line = br.readLine()) != null) {
            MLTDBgInfo bgInfo = new MLTDBgInfo();
            String[] infoGroup = line.split("-");
            bgInfo.setId(infoGroup[1]);
            bgInfo.setEventName(infoGroup[2].substring(0, infoGroup[2].lastIndexOf('.')));
            bgInfo.setName(line);
            bgInfo.setUrl(address + URLEncoder.encode(line, "UTF-8").replaceAll("\\+", "%20"));
            bgInfoList.add(bgInfo);
        }
        br.close();
    }

}
